$("#change").on('click',function(){
    $('#contact-form').toggle()
    $('#contact-form2').toggle()
    if($('#hh').text()=='注册'){
        $('#hh').text('登录')
        $('#h').text('注册')
    }else{
        $('#hh').text('注册')
        $('#h').text('登录')
    }
})
//登录
$("#contact-form").children('button').on('click',function(){
    let yhname = $("#contact-form").children('.row').find('input').eq(0).val()
    let yhpwd = $("#contact-form").children('.row').find('input').eq(1).val()
    $.ajax({
        type: "POST",
        url: "/api/admin/login",
        data: {
            loginId: yhname,
            loginPwd: yhpwd
        },
        success:(data)=>{
            console.log(data.data)
            if(data.data === null){
                $('#login1').html('用户名或密码错误')
            }else if(data.data){
                alert('登录成功')
                window.location.reload()
            }
        }
    })
})

//设置cookie
function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires+"; path=/";//path=/是根路径
}

//获取cookie
function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1);
        if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
    }
    return "";
}

//删除cookie
function clearCookie(name) {
    setCookie(name, "", -1);
}

//注册
$("#contact-form2").children('button').on('click',function(){
    const user= $("#contact-form2").children('.row').find('input').eq(0).val()
    const name= $("#contact-form2").children('.row').find('input').eq(1).val()
    const userpwd= $("#contact-form2").children('.row').find('input').eq(2).val()
    const surepwd= $("#contact-form2").children('.row').find('input').eq(3).val()
    const img= $("#contact-form2").children('.row').find('input').eq(4)[0].files
    //构建formdata格式的参数                            
    const formData = new FormData()
    for (const file of img) {
        formData.append("img",file,file.name)
    }
    console.log(formData)
    $.ajax({
        type:"POST",
        url:"/api/upload",
        data:formData,
        contentType: false,
        // 告诉jQuery不要去设置Content-Type请求头
        processData: false,
        // 告诉jQuery不要去处理发送的数据
        success:(data)=>{
            console.log(data.data)
            const a=data.data
            $.ajax({
                type:"POST",
                url:"/api/admin/register",
                data: {
                    loginId: user,
                    loginPwd: surepwd,
                    name:name,
                    img:`http://localhost:3300${a}`,
                },
                success:(data)=>{
                    console.log(data.data)
                    if(data.data){
                        const user = $("#contact-form2").children('.row').find('input').eq(0).val('')
                        const name = $("#contact-form2").children('.row').find('input').eq(1).val('')
                        const userpwd = $("#contact-form2").children('.row').find('input').eq(2).val('')
                        const surepwd = $("#contact-form2").children('.row').find('input').eq(3).val('')
                        alert("注册成功！")
                    }    
                }
            })
        },
    })
})